package cn.xfdzx.android.apps.shop.activity;


import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.OnClick;
import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.activity.login.OneLoginMainActivity;
import cn.xfdzx.android.apps.shop.adapter.FamilyGoodListAdapter;
import cn.xfdzx.android.apps.shop.apps.Constants;
import cn.xfdzx.android.apps.shop.base.BaseMvpActivity;
import cn.xfdzx.android.apps.shop.bean.ConfirmGoodBeen;
import cn.xfdzx.android.apps.shop.bean.FamliyItemBeen;
import cn.xfdzx.android.apps.shop.contract.FamilyContract;
import cn.xfdzx.android.apps.shop.evenbus.CarInitDataMessage;
import cn.xfdzx.android.apps.shop.evenbus.FamilyRefreshMessage;
import cn.xfdzx.android.apps.shop.presenter.FamilyPresenter;
import cn.xfdzx.android.apps.shop.util.ProgressDialog;
import cn.xfdzx.android.apps.shop.util.UtilsData;

import static cn.xfdzx.android.apps.shop.apps.Constants.APP_TOKEN;


public class FamilyBuyActivity extends BaseMvpActivity<FamilyPresenter> implements FamilyContract.View {


    private RecyclerView rlList;
    private FamilyGoodListAdapter familyGoodListAdapter;
    private String limit = "10";
    private int page = 1;
    private List<FamliyItemBeen.DataBean> mList = new ArrayList();
    SmartRefreshLayout mRefreshLayout;
    private TextView tvCarNum;
    private ImageView ivShopCar;
    private ImageView ivSetTop;
    private ScrollView svView;

    @Override
    public int getLayoutId() {
        return R.layout.activity_family_buy;
    }

    @Override
    public void initView() {
        EventBus.getDefault().register(this);
        mPresenter = new FamilyPresenter();
        mPresenter.attachView(this);

        rlList = findViewById(R.id.rl_list);
        tvCarNum = findViewById(R.id.tv_car_num);
        ivShopCar = findViewById(R.id.iv_shop_car);
        ivSetTop = findViewById(R.id.iv_set_top);
        mRefreshLayout = findViewById(R.id.refreshLayout);
        svView = findViewById(R.id.sv_view);


        rlList.setLayoutManager(new LinearLayoutManager(this));
        rlList.setItemAnimator(new DefaultItemAnimator());
        rlList.setNestedScrollingEnabled(false);
        familyGoodListAdapter = new FamilyGoodListAdapter(mList, mPresenter);


        rlList.setAdapter(familyGoodListAdapter);
        View headView = LayoutInflater.from(this).inflate(R.layout.item_family_head, rlList, false);

        familyGoodListAdapter.addHeaderView(headView);

        mPresenter.initData(true, true, page + "", limit + "");

        int num = UtilsData.getPreference(this, "carCount").getInteger("car_ls_count");
        if (num <= 0) {
            tvCarNum.setVisibility(View.INVISIBLE);
            tvCarNum.setText(num + "");
        } else {
            tvCarNum.setVisibility(View.VISIBLE);
            tvCarNum.setText(num + "");
        }
        ivShopCar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(FamilyBuyActivity.this, GoodCarActivity.class);
                intent.putExtra("type", 0);
                startActivity(intent);
            }
        });
        ivSetTop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                svView.scrollTo(0, 0);
            }
        });

        mRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                mPresenter.initData(true, true, page + "", limit + "");
            }
        });
    }

    @Override
    public void showLoading() {
        if (!ProgressDialog.getInstance().materialDialog.isShowing()) {
            ProgressDialog.getInstance().show(this);
        }
    }

    @Override
    public void hideLoading() {
        ProgressDialog.getInstance().dismiss();
    }

    @Override
    protected boolean isUseFullScreenMode() {
        return true;
    }

    @Override
    public void onError(Throwable throwable) {

    }

    @Subscribe(threadMode = ThreadMode.POSTING)
    public void XXX(FamilyRefreshMessage familyRefreshMessage) {
        for (FamliyItemBeen.DataBean dataBean:mList){
            if (dataBean.getGoods_id().equals(familyRefreshMessage.getGood_id())){
                dataBean.setCart_num(familyRefreshMessage.getGood_num());
                familyGoodListAdapter.notifyDataSetChanged();
            }
        }
    }

    @Override
    public void onInitSuccess(FamliyItemBeen bean) {
        if (bean.getStatus() == Constants.SUCCESS_CODE) {
            if (page == 1) {
                mList.clear();
                mRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
                    @Override
                    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                        mPresenter.initData(false, false, page + "", limit + "");
                    }
                });
            }
            if (bean.getData().size() == 0) {
                mRefreshLayout.finishLoadMoreWithNoMoreData();
            } else {
                page++;
            }
            mList.addAll(bean.getData());
            familyGoodListAdapter.notifyDataSetChanged();

        } else {
            if (bean.getStatus() == Constants.LOGIN_CODE) {
                UtilsData.getPreference(this, "login").remove("head_url");
                UtilsData.getPreference(this, "login").remove("nickname");
                UtilsData.getPreference(this, "login").remove("is_store");
                UtilsData.getPreference(this, "login").remove("token");
                APP_TOKEN = "";
                Intent intent = new Intent(this, OneLoginMainActivity.class);
                startActivity(intent);
                return;
            }
            Toast.makeText(this, bean.getMsg(), Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onAddCarSuccess(ConfirmGoodBeen bean, int position, int total_num) {
        if (bean.getStatus() == Constants.SUCCESS_CODE) {
            Toast.makeText(this, "加入购物车成功", Toast.LENGTH_SHORT).show();
            mList.get(position).setCart_num(total_num);
            familyGoodListAdapter.notifyDataSetChanged();
            tvCarNum.setText(bean.getData().getCount().getPfcount() + "");

        } else {
            if (bean.getStatus() == Constants.LOGIN_CODE) {
                UtilsData.getPreference(this, "login").remove("head_url");
                UtilsData.getPreference(this, "login").remove("nickname");
                UtilsData.getPreference(this, "login").remove("is_store");
                UtilsData.getPreference(this, "login").remove("token");
                APP_TOKEN = "";
                Intent intent = new Intent(this, OneLoginMainActivity.class);
                startActivity(intent);
                return;
            }
            Toast.makeText(this, bean.getMsg(), Toast.LENGTH_SHORT).show();
        }

    }

    @Override
    public void finishRefresh() {
        mRefreshLayout.finishRefresh(true);//刷新完成
    }

    @Override
    public void finishLoadMore() {
        mRefreshLayout.finishLoadMore(true);//加载完成
    }


    @OnClick({R.id.iv_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;

        }
    }

    @Override
    protected void onDestroy() {
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
        super.onDestroy();
    }

}
